#! /usr/bin/python

import wx
import serial

import random

ID_ABOUT = 101
ID_EXIT = 110
ID_HELP = 100
ID_SCORES = 111
ID_STATIC_BOX = 1000

Patterns = ['xxxxxxxx\nxxxxxxxx\nxxxxxxxx\nxxxxxxxx\nxxxxxxxx\nxxxxxxxx\nxxxxxxxx\nxxxxxxxx\n', 
            'x000000x\n0x0000x0\n00x00x00\n000xx000\n000xx000\n00x00x00\n0x0000x0\nx000000x\n',
            '000xx000\n000xx000\n000xx000\nxxxxxxxx\nxxxxxxxx\n000xx000\n000xx000\n000xx000\n', 
            'xxxxxxxx\nx000000x\nx000000x\nx000000x\nx000000x\nx000000x\nx000000x\nxxxxxxxx\n']

pattern = ""
GameOver = False
GameStarted = False
Reset = False
GameState = "Game Status waiting"

Scores = { "Player1": 0, "Player2": 0, "Player3": 0, "Player4": 0 }

serport = serial.Serial( port = "/dev/ttyUSB0", baudrate = 115200, timeout = 2 )

class sketchItMainWindow( wx.Frame ):
    def __init__( self, parent, id, title ):
        wx.Frame.__init__( self, parent, wx.ID_ANY, title, size = ( 960, 800 ), style = wx.DEFAULT_FRAME_STYLE | wx.NO_FULL_REPAINT_ON_RESIZE )
        bmpImage = wx.Image( name = "monome-splash.jpg" ).ConvertToBitmap()
        self.sketchItSplash = wx.SplashScreen( bmpImage, wx.SPLASH_CENTRE_ON_PARENT | wx.SPLASH_TIMEOUT, 3000, parent )
        self.CreateStatusBar()
        self.timer = wx.Timer( self )
        self.Bind( wx.EVT_TIMER, self.ReadSerial, self.timer )
        fileMenu = wx.Menu()
        aboutMenu = wx.Menu()
        aboutMenu.Append( ID_HELP, "&Help", "Help menu" )
        aboutMenu.Append( ID_ABOUT, "&About", "About Sketch It!" )
        fileMenu.Append( ID_SCORES, "&Scores", "Check out the High scores in Sketch It!" )
        fileMenu.AppendSeparator()
        fileMenu.Append( ID_EXIT, "&Quit", "Quit Sketch It!" )
        sketchItMenuBar = wx.MenuBar()
        sketchItMenuBar.Append( fileMenu, "&File" )
        sketchItMenuBar.Append( aboutMenu, "&About" )
        self.SetMenuBar( sketchItMenuBar )
        wx.EVT_MENU( self, ID_SCORES, self.OnScores )
        wx.EVT_MENU( self, ID_HELP, self.OnHelp )
        wx.EVT_MENU( self, ID_ABOUT, self.OnAbout )
        wx.EVT_MENU( self, ID_EXIT, self.OnQuit )
        self.timer.Start( 50 )
        self.Show( True )

    def OnScores( self, event ):
        ScoreDisplay = "Player 1: " + str(Scores["Player1"]) + "\nPlayer 2: " + str(Scores["Player2"]) + "\nPlayer 3: " + str(Scores["Player3"]) \
            + "\nPlayer 4: " + str(Scores["Player4"]) + "\n"
        ScoreBox = wx.MessageDialog( self, ScoreDisplay, "High Scores", wx.OK)
        ScoreBox.ShowModal()
        ScoreBox.Destroy()

    def OnHelp( self, event ):
        dbox = wx.MessageDialog( self, "Goal of the game:\n\nDraw the pattern that is displayed on the console.\n" \
                                     "\nIn the cooperative mode, work along with your teammate, share the pattern as you want" \
                                     "but finally get the pattern onto one board.\n" \
                                     "\nIn the competitive mode, compete with each other either as teams or individually." \
                                     "The last one to get the pattern is a rotten egg!", "Help Menu", wx.OK )
        dbox.ShowModal()
        dbox.Destroy()

    def OnAbout( self, event ):
        dbox = wx.MessageDialog( self, "Sketch It! is a multiplayer pattern matching game created as a semester project for ESE519 course at" \
                                     " the University of Pennsylvania.", "About Sketch It!", wx.OK )
        dbox.ShowModal()
        dbox.Destroy()

    def OnQuit( self, event ):
        self.Close( True )
    
    def ReadSerial( self, event ):
        GameStatusBox = wx.StaticBox( self, label = 'Real Time Game Status', id = -1, pos = ( 0, 0 ), size = ( 475, 475 ), name = "GameStatusBox" )
#        if ( serport.inWaiting() > 0 ):
        x = serport.readline()
        pattern = self.parseData(x)

        if ( GameStarted ):
            GameState = "Game Started\n"
        elif ( GameOver ):
            GameState = "Game Over\n"
        elif ( Reset ):
            GameState = "Game Reset\n"
        else:
            GameState = "Yet to start game\n"

        if ( type(pattern) != int and pattern[:7] == "Synched" ):
            control_pl1 = wx.TextCtrl( self, -1, pattern, size = (200, 200), pos = (28, 30), style = wx.TE_READONLY | wx.TE_MULTILINE | wx.TE_CENTRE )
            control_pl2 = wx.TextCtrl( self, -1, pattern, size = (200, 200), pos = (250, 30), style = wx.TE_READONLY | wx.TE_MULTILINE | wx.TE_CENTRE )
            control_pl3 = wx.TextCtrl( self, -1, pattern, size = (200, 200), pos = (28, 250), style = wx.TE_READONLY | wx.TE_MULTILINE | wx.TE_CENTRE )
            control_pl4 = wx.TextCtrl( self, -1, pattern, size = (200, 200), pos = (250, 250), style = wx.TE_READONLY | wx.TE_MULTILINE | wx.TE_CENTRE )

        if ( type(pattern) == int ):
            BasePatternBox = wx.StaticBox( self, label = 'Base Pattern', id = -1, pos = ( 485, 0 ), size = ( 475, 475 ), name = "BasePatternBox" )
            control_BP = wx.TextCtrl( self, -1, Patterns[pattern], size = (430, 420), pos = (510, 30), style = wx.TE_READONLY | wx.TE_MULTILINE | wx.TE_CENTRE )
        
        if ( pattern == "Game Started" or pattern == "Game Over" or "Gameplay" in pattern or "State" in pattern ):
            GameStateBox = wx.StaticBox( self, label = 'Game State', id = -1, pos = ( 0, 480 ), size = ( 960, 190 ), name = "GameStateBox" )
            control_GS = wx.TextCtrl( self, -1, pattern, size = (800, 100), pos = (70, 530), style = wx.TE_CENTRE | wx.TE_MULTILINE | wx.TE_READONLY )

    def parseData( self, x ):
        print x[0]
        print x[-3]
        pattern = ""
        if x[:7] == 'Synched':
            pattern = "Synched player" + x[-3] + "\n"
        if x[:7] == 'Maximum':
            pattern = "Maximum of 4 players synched"
        if x[:8] == 'Gameplay':
            if x[-3] == '0':
                pattern = "Gameplay selected: " + x[-5] + "player Competitive"
            if x[-3] == '1':
                pattern = "Gameplay selected: " + x[-5] + "player Cooperative"
        if x[:5] == 'Start':
            pattern = "Game Started"
        if x[:7] == 'Pattern':
            if x[-3] == '0':
                pattern = 0
            if x[-3] == '1':
                pattern = 1
            if x[-3] == '2':
                pattern = 2
            if x[-3] == '3':
                pattern = 3
        if 'Gameover' in x:
            pattern = "Game Over"
            if ( len( x ) > 12):
                pattern += x[8:]
                winningPlayer = "Player" + x[-3]
                Scores["winningPlayer"] += 10
            else:
                pattern += "Game timed out\n"
        if x[:5] == 'state' and x[-3] == '5':
            pattern = "Game Over"
        if x[:5] == 'state' and x[-3] == '6':
            pattern = "Game Over"
        if x[:5] == 'state' and x[-3] == '0':
            pattern = "Sync State"
        if x[:5] == 'state' and x[-3] == '1':
            pattern = "Select State"
        if x[:5] == 'state' and x[-3] == '2':
            pattern = "Pre-game State"
        if x[:5] == 'state' and x[-3] == '3':
            pattern = "In Game State"
        return pattern


app = wx.PySimpleApp()
frame = sketchItMainWindow( None, -1, "Sketch It!" )

# for p in Patterns:
#     for p1 in p:
        
app.MainLoop()
